#define DEBUG_TOUCHSCREEN_

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System.Diagnostics;

namespace WPCoco.Controllers
{
    public class TouchScreen : Controller
    {
        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                base.Enabled = value;
            }
        }

        bool isDown = false;

        public bool IsDown
        {
            get { return isDown; }
            private set { isDown = value; }
        }

        public override void Update(GameTime gameTime, TouchInputState touch)
        {
            GestureSample sample;
            for (int i = 0; i < touch.GestureSamples.Count; i++)
            {
                sample = touch.GestureSamples[i];  
                switch (sample.GestureType)
                {
                    case GestureType.DoubleTap:
                        Raise(DoubleTap, sample);
                        break;
                    case GestureType.DragComplete:
                        Raise(EndDrag, sample);
                        isDown = false;
                        break;
                    case GestureType.Flick:
                        Raise(Flick, sample);
                        break;
                    case GestureType.VerticalDrag:
                    case GestureType.HorizontalDrag:
                    case GestureType.FreeDrag:
                        if (!isDown)
                        {
                            isDown = true;
                            Raise(StartDrag, sample);
                        }
                        else
                        {
                            Raise(Dragging, sample);
                        }
                        break;
                    case GestureType.Tap:
                        Raise(Tap, sample);
                        break;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            
        }


        void Raise(EventHandler<CustomEventArgs<GestureSample>> handler, GestureSample sample)
        {
            if (handler == null) return;
            handler(this, new CustomEventArgs<GestureSample>(sample));

#if DEBUG_TOUCHSCREEN
            Delegate[] ilist = handler.GetInvocationList();
            Debug.WriteLine("TOUCH SCREEN INVOCATION COUNT: {0}", ilist.Length);
#endif
        }

        public event EventHandler<CustomEventArgs<GestureSample>> StartDrag;
        public event EventHandler<CustomEventArgs<GestureSample>> Dragging;
        public event EventHandler<CustomEventArgs<GestureSample>> EndDrag;

        public event EventHandler<CustomEventArgs<GestureSample>> Tap;
        public event EventHandler<CustomEventArgs<GestureSample>> DoubleTap;

        public event EventHandler<CustomEventArgs<GestureSample>> Flick;
    }
}
